﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TN_CMS.Admin.Helper.Common;
using TN_CMS.Admin.Models;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Concrete;
using TN_CMS.Domain.Entities;
using TN_CMS.Framework.Helper;

namespace TN_CMS.Admin.Controllers
{
    public class ProductController : TNController
    {
        private readonly IProductRepository _productRepository;
        private readonly ICommonRepository _commonRepository;

        public ProductController(
            IProductRepository productRepository,
            ICommonRepository commonRepository)
        {
            this._productRepository = productRepository;
            this._commonRepository = commonRepository;
        }

        #region Product

        public ActionResult List()
        {
            var model = new Product();

            int total = 0;
            var lstGroup = this._productRepository.GetListGroup(
                0,
                Int16.MaxValue,
                out total,
                "order by C.Id",
                "and C.IsActive = 1 "
            );
            ViewBag.LstGroup = new SelectList(lstGroup, "Id", "Name");

            return View(model);
        }

        [HttpPost]
        public ActionResult GetListProduct(int pageIndex, string orderClause)
        {
            var result = new CustomJsonResult();
            int total = 0;
            int pageSize = AdminHelper.GetPageSize();

            try
            {
                string whereClause = string.Empty;

                var lst = this._productRepository.GetList(
                    pageIndex,
                    pageSize,
                    out total,
                    orderClause,
                    whereClause
                );
                result.Result = lst;
                result.Optional = total;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetDetailProduct(int id)
        {
            var result = new CustomJsonResult();

            try
            {
                var detail = new Product();

                if (id != -1)
                {
                    detail = this._productRepository.GetDetail(id);
                }
                else
                {
                    detail.Id = -1;
                    detail.IsActive = true;
                }

                result.Result = detail;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManageProduct(Product detail)
        {
            var result = new CustomJsonResult();

            try
            {
                if (detail.Id == -1)
                {
                    // create
                    detail.CreatedBy = this.SysAdmin.Id;
                    detail.Id = this._productRepository.Insert(detail);
                }
                else
                {
                    // edit
                    detail.UpdatedBy = this.SysAdmin.Id;
                    this._productRepository.Update(detail);
                }

                // delete mapping
                this._commonRepository.Delete(
                    "ProductProductGroupMapping",
                    "ProductId",
                    Convert.ToString(detail.Id)
                );

                // insert mapping
                if (detail.LstGroup != null && detail.LstGroup.Count > 0)
                {
                    foreach (int groupId in detail.LstGroup)
                    {
                        this._commonRepository.InsertMapping(
                            "ProductProductGroupMapping",
                            "ProductId",
                            "ProductGroupId",
                            detail.Id,
                            groupId
                        );
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateActiveProduct(int[] arrId, bool isActive)
        {
            var result = new CustomJsonResult();

            try
            {
                this._commonRepository.UpdateActive(
                    EFProductRepository.TableProducts,
                    EFProductRepository.ColumnIsActive,
                    EFProductRepository.ColumnId,
                    CommonHelper.ConvertArrIdToStr(arrId),
                    isActive
                );
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteProduct(int[] arrId)
        {
            var result = new CustomJsonResult();

            try
            {
                this._commonRepository.Delete(
                    EFProductRepository.TableProducts,
                    EFProductRepository.ColumnId,
                    CommonHelper.ConvertArrIdToStr(arrId)
                );
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        #endregion

        #region Product Group

        public ActionResult Group()
        {
            var model = new ProductGroup();

            return View(model);
        }

        [HttpPost]
        public ActionResult GetListProductGroup(int pageIndex, string orderClause)
        {
            var result = new CustomJsonResult();
            int total = 0;
            int pageSize = AdminHelper.GetPageSize();

            try
            {
                string whereClause = string.Empty;
                
                var lst = this._productRepository.GetListGroup(
                    pageIndex,
                    pageSize,
                    out total,
                    orderClause,
                    whereClause
                );
                result.Result = lst;
                result.Optional = total;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetDetailProductGroup(int id)
        {
            var result = new CustomJsonResult();

            try
            {
                var detail = new ProductGroup();

                if (id != -1)
                {
                    detail = this._productRepository.GetDetailGroup(id);
                }
                else
                {
                    detail.Id = -1;
                    detail.IsActive = true;
                }

                result.Result = detail;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManageProductGroup(ProductGroup detail)
        {
            var result = new CustomJsonResult();

            try
            {
                if (detail.Id == -1)
                {
                    // create
                    detail.CreatedBy = this.SysAdmin.Id;
                    this._productRepository.InsertGroup(detail);
                }
                else
                {
                    // edit
                    detail.UpdatedBy = this.SysAdmin.Id;
                    this._productRepository.UpdateGroup(detail);
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateActiveProductGroup(int[] arrId, bool isActive)
        {
            var result = new CustomJsonResult();

            try
            {
                this._commonRepository.UpdateActive(
                    EFProductRepository.TableProductGroups,
                    EFProductRepository.ColumnIsActive,
                    EFProductRepository.ColumnId,
                    CommonHelper.ConvertArrIdToStr(arrId),
                    isActive
                );
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteProductGroup(int[] arrId)
        {
            var result = new CustomJsonResult();

            try
            {
                this._commonRepository.Delete(
                    EFProductRepository.TableProductGroups,
                    EFProductRepository.ColumnId,
                    CommonHelper.ConvertArrIdToStr(arrId)
                );
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        #endregion
    }
}
